package com.ling.core.view.builder;

import com.bstek.dorado.view.widget.form.autoform.AutoForm;
import com.ling.core.view.ViewComponent;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class AutoFormBuilder extends AbstractControlBuilder {

    public void build(Object control, ViewComponent parentViewComponent) {
        AutoForm autoForm = (AutoForm) control;
        String id = autoForm.getId();
        ViewComponent component = new ViewComponent();
        component.setId(id);
        component.setIcon(">dorado/res/" + AutoForm.class.getName().replaceAll("\\.", "/") + ".png");
        component.setName(AutoForm.class.getSimpleName());
        if (StringUtils.isEmpty(id)) {
            component.setEnabled(false);
        }
        parentViewComponent.addChildren(component);

        for (com.bstek.dorado.view.widget.Component c : autoForm.getElements()) {
            for (IControlBuilder builder : builders) {
                if (builder.support(c)) {
                    builder.build(c, component);
                    break;
                }
            }
        }
    }

    public boolean support(Object control) {
        return control instanceof AutoForm;
    }

}
